package com.cluster.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.common.api.ResultCode;
import com.cluster.platform.dto.CmsChannelInfoDto;
import com.cluster.platform.mapper.CmsChannelChildMapper;
import com.cluster.platform.mapper.CmsChannelInfoMapper;
import com.cluster.platform.model.CmsChannelChild;
import com.cluster.platform.model.CmsChannelInfo;
import com.cluster.platform.service.CmsChannelInfoService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: CmsChannelInfoServiceImpl
 */
@Service
public class CmsChannelInfoServiceImpl implements CmsChannelInfoService {

    @Autowired
    private CmsChannelInfoMapper cmsChannelInfoMapper;

    @Autowired
    CmsChannelChildMapper cmsChannelChildMapper;

    @Override
    public CommonResult<Object> searchChannelPage(CmsChannelInfoDto cmsChannelInfoDto) {
        if (null == cmsChannelInfoDto) {
            return CommonResult.failed("未获取参数信息");
        }
        PageHelper.startPage(cmsChannelInfoDto.getPageNum(), cmsChannelInfoDto.getPageSize());
        CmsChannelInfo cmsChannelInfo = BeanUtil.copyProperties(cmsChannelInfoDto, CmsChannelInfo.class);
        List<CmsChannelInfo> cmsChannelInfos = cmsChannelInfoMapper.searchCmsChannelInfo(cmsChannelInfo);
        if (cmsChannelInfos.isEmpty()) {
            CommonResult.failed("为查询到信息");
        }
        return CommonResult.success(CommonPage.restPage(cmsChannelInfos));
    }

    @Override
    @Transactional
    public CommonResult<Object> addChannel(CmsChannelInfo cmsChannelInfo) {
        if (null == cmsChannelInfo) {
            return CommonResult.failed("未获取参数信息");
        }
        cmsChannelInfo.setStatus(1);
        cmsChannelInfo.setInsertDate(new Date());
        int insertSelective = cmsChannelInfoMapper.insertSelective(cmsChannelInfo);
        if (insertSelective < 0) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success(cmsChannelInfo);
    }

    @Override
    @Transactional
    public CommonResult<Object> updateChannel(CmsChannelInfo cmsChannelInfo) {
        if (null == cmsChannelInfo) {
            return CommonResult.failed("未获取参数信息");
        }
        int update = cmsChannelInfoMapper.updateByPrimaryKeySelective(cmsChannelInfo);
        if (update < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(cmsChannelInfo);
    }

    @Override
    @Transactional
    public CommonResult<Object> delChannel(CmsChannelInfo cmsChannelInfo) {
        if (null == cmsChannelInfo) {
            return CommonResult.failed("未获取参数信息");
        }
        cmsChannelInfo.setStatus(0);
        int update = cmsChannelInfoMapper.updateByPrimaryKeySelective(cmsChannelInfo);
        if (update < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(cmsChannelInfo);
    }

    @Override
    @Transactional
    public CommonResult<Object> batchChannel(Integer status, List<String> ids) {
        if (ids.isEmpty()) {
            return CommonResult.failed("操作失败");
        }
        int updateBatch = cmsChannelInfoMapper.batchChannel(status, ids);
        if (updateBatch < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(updateBatch);
    }

    @Override
    public CommonResult<Object> getChannelTree() {
        List<CmsChannelInfo> channel = cmsChannelInfoMapper.getChannel();
        if (channel.isEmpty()) {
            return CommonResult.success(ResultCode.NO_RECORD);
        }
        List<CmsChannelInfoDto> cmsChannelInfoDtos = new ArrayList<>();
        for (CmsChannelInfo cmschannelInfo : channel) {
            CmsChannelInfoDto cmsChannelInfoDto = BeanUtil.copyProperties(cmschannelInfo, CmsChannelInfoDto.class);
            cmsChannelInfoDto.setCmsChannelChildren(cmsChannelChildMapper.searchColumnsChild(cmschannelInfo.getId()));
            cmsChannelInfoDtos.add(cmsChannelInfoDto);
        }
        return CommonResult.success(cmsChannelInfoDtos);
    }

    @Override
    public CommonResult<Object> getChannelList() {
        List<CmsChannelInfo> channel = cmsChannelInfoMapper.getChannel();
        if (channel.isEmpty()) {
            return CommonResult.success(ResultCode.NO_RECORD);
        }
        return CommonResult.success(channel);
    }
}
